COM Part 3

Here's the long awaited thrid installment of a tutorial series which I've been told sucks. I really should have made a point that like DLLS, COM Objects require a lot of thinking you don't come across in every day situations. You shouldn't play with COM Objects unless you understand Object-Oriented programming concepts. You should know Javascript, or something far better, at the very least. This would allow you to be at least familiar with what an object is, and what you can do with this style of coding.

Find this before you begin:
Windows Scriptlets Wizard
Windows Scripting Host 5.6 Documentation [and the associated stuff might be handy too]

COM Objects: Making and Breaking

To create a com object you need an expensive programming tool. Merp. WRONG! You don't need Visual Basic or Visual C++ to create com objects. These tools cost money, which many of you don't have. So here's the cheep way. Be warned: Its hardly effective.

Windows Scripting Host

Alright, alright, its a virus prone thing. Many people still have it though. There's an effort to make this a more powerful tool, also. Its called 'Scriptlets'.

There's not much going on at Microsoft HQ about developing these, but there is something. You can create COM Objects using Visual Basic Script, or JavaScript(JScript), or anything supported by Windows Scripting Host (This includes anything from REXXscript to Perlscript). This means there's a chance you can program fir mIRC in your choice of language.

Right now, however, I'm going to refer you to the Windows Scriptlets Documentation. You will need to hunt around on msdn to find the following:
Windows Scriptlets Wizard [VITAL!]
Windows Scripting Host 5.6 Documentation [and the associated stuff might be handy too]
As I find the MSDN site to suck, majorly, I suggest using google.

You'd best get familiar with what's going on, as we are about to make quite a leap.

Baby's first rattle, Coder's first COM Object

Hopefully you have everything installed nicely. You probably upgraded scrObj.dll at some point, and have a nice shiny new shortcut in your start menu.

Run the scriptlet wizard.

Nothing too hard so far, eh? A five step thing, you'll notice. Fill out the info as follows:
Name:Rattle
Filename:Rattle
Prog Id:Rattle.Scriptlet
Version:1.00
Location:C:\path\
Next
Where do you want the scriplet to run?Server
Language:JScript [for now at least!]
Format:Spaces [I like soft tabs!]
Create Title:Yes
Next
Here we get a little tricky. We are defining properties, which are just information [they don't do anything]. Create the following properties.
Name:Weight
Type:Read/Write
Default:1
Name:Color
Type:Read
Default:Red
Name:Size
Type:Write
Default:
You'll notice that "Color" cannot be changed, but all the others can. "Size" cannot be read, so we will never know what value was there, we can overright it with our own and remember that. "Weight" can do both.
Next
Here we get a little trickier. We are defining methods, which are more or less aliases. They are the "doing" things.
Name:Rattle
Parameters:N
Name:Throw
Parameters:
Next Take note of the summary. Its nice to write down your information, but not critical. This is what I have at this moment.

Scriptlet
	Rattle
	Version 1.00
	C:\#mIRCpond\mear\Rattle.sct

Registration
	Rattle.Scriptlet
	{e0d5c8c0-bbe9-11d7-ab3c-525400edd75c}

Characteristics
	Server
	JScript
	4 Spaces
	Create a title

Properties
	Wieght	Read / Write	1
	Color	Read-Only	Red
	Size	Write-Only	

Methods
	Rattle	N
	Throw	
Finish

There's the template for the scriplet done. That was easy, hey!

Making the code

Open up the Rattle.sct file in the path you specified. You can also look in the sample version of it included with this package Rattle(Template).sct. You'll see a bunch of XML, and some script tags. Look at the script bits.

var Wieght = 1;
var Color = "Red";
var Size;

function get_Wieght()
{
    return Wieght;
}

function put_Wieght(newValue)
{
    Wieght = newValue;
}

function get_Color()
{
    return Color;
}

function put_Size(newValue)
{
    Size = newValue;
}

function Rattle(N)
{
    return "Temporary Value";
}

function Throw()
{
    return "Temporary Value";
}

You've got your prototyped methods there. Time, however, to modify them. All of the get_*() are known as the Accessor methods, for the properties. These allow you to format data, or something like it.

;  Its like doing:
alias getVersion {
	echo -s Debugging something, or other action
	return The Version is %version by %author
}

//echo -s $getVersion | msg $active $getVersion

;  rather than:

//echo -s Debugging something, or other action
//echo -s The Version is %version by %author 
//echo -s Debugging something, or other action
//msg $active The Version is %version by %author 

;Much quicker, isn't it.

Anyway, we have digressed a little. We want to leave these alone for now, same with the set_*() methods. The put_*() are known as Mutators. These allow you to check input to see if its valid, or translate it, or whatever.

;  Its like doing:
alias setVersion {
	if ($2 == bob) { return BOB is invalid input! }
	else {
		set %version $1
		set %author $2
	}
}

;  rather than:
set %version 1.00
set %author franknotbob

Now we look at the main methods. Rattle(N), and Throw(). First, Rattle(N). N is a parameter, think of it as a varible. Modify the code to the following:

function Rattle(N)
{
    return "Rattled " + N + " times. That's a lot since it is so heavy - " + get_Wieght() + "kg!";
}
Next, make Throw() do this:
function Throw()
{
    return "You threw the rattle. It fell in a bucket of paint and is no longer " + get_Color();
}

Save it. Go into explorer and right click on the little bastard. Pick "Register". Hopefully you get "DLL blah and more blah succeeded!"

Using the COM Object

From now on in should be easy sailing.

alias rattle { 
  comopen rattle Rattle.Scriptlet

  if ($comerr) { 
    echo comopen failed 
    halt 
  } 

;Get The Color of the "Rattle" Object
  if ($com(rattle,Color,3) == 0) { 
    echo $com failed 
    goto finish 
  } 
  echo Color: $com(rattle).result 

;Get The Weight of the "Rattle" Object
  if ($com(rattle,Wieght,3) == 0) { 
    echo $com failed 
    goto finish 
  } 
  echo Wieght: $com(rattle).result 

;CHANGE he Weight of the "Rattle" Object
  if ($com(rattle,Wieght,4,i4,2) == 0) { 
    echo $com failed 
    goto finish 
  } 

;Get The Weight of the "Rattle" Object
  if ($com(rattle,Wieght,3) == 0) { 
    echo $com failed 
    goto finish 
  } 
  echo Wieght: $com(rattle).result 

;Rattle it 10 times - Rattle(N) method
  if ($com(rattle,Rattle,1,string,10) == 0) { 
    echo $com failed 
    goto finish 
  }
  echo $com(rattle).result 

;Throw it - Throw() method
  if ($com(rattle,Throw,1) == 0) { 
    echo $com failed 
    goto finish 
  }
  echo $com(rattle).result 

  :finish 
  comclose rattle
}

Conclusion

Yes I know I misspelt Wieght. I kept doing it too after I realized. Deal with it!

Where to go from here. Well, this only starts you off a STEEP learning curve about object orientated programming - there are many books on the topic, but the best way to learn is Shut Up And Do It. ITs how I managed, and once you get it all set in your head langauges like PHP, Java, Visual Basic, Even C++ are a lot easier to effectively code in.

Ideas for the Future

Now you have a far more powerful tool than mirc script alone at your hands. You can make almost anything imagineable as a Com Object, wheter its an encryption method or its something else. The trick to stopping your scripts from being ripped (if you don't want CERTAIN bits ripped) is to use Microsoft's script encoder - you can (weakly) encrypt all of your code, and some non-critical passwords, methods, whatever. However, I warn you, the script encoder HAS been broken by the folks at Astalavista.com, so its nevr 100% secure.

Additionally - its all plain text source - you can write and REWRITE on the fly the entire code, you can send it over plain IRC in a privmsg for godsake. Its just XML and javascript! Wow. Experiment, people!

Included are samples of the template, the completed scriptlet, and the alias "Rattle" for mIRC

Reference Texts

The following reference texts can help a helluva lot in developing COM Objects and OOP

COM:
Windows Scripting Host Documentation 5.6
Windows Scriptlet Documentation

OOP:
"Java 2: The Complete Reference (4th Edition)" by Herbert Schildt (www.osborne.com).

Java is a great language to learn, as its compiler is free, it works anywhere, at its powerful! Not to mention entriely Object Orientated.